package com.thb.pay.core.utils;

import com.xiaoleilu.hutool.collection.CollUtil;
import lombok.ToString;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;


@ToString
public class PageResult<T> implements Serializable {

    private List<T> records;

    private long total;

    private long size;

    private long current;

    private long pages;

    public PageResult() {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
    }

    public PageResult(long current, long size) {
        this(current, size, 0L);
    }

    public PageResult(long current, long size, long total) {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        if (current > 1L) {
            this.current = current;
        }

        this.size = size;
        this.total = total;
    }


    public PageResult(long current, long size, long total, List<T> records) {
        this.records = records != null ? records : Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        if (current > 1L) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
        this.pages = getPages();
    }

    public boolean hasPrevious() {
        return this.current > 1L;
    }

    public boolean hasNext() {
        return this.current < this.getPages();
    }

    public long getPrePage() {
        if (hasPrevious()) {
            return this.current - 1;
        }
        return 0L;
    }

    public long getNextPage() {
        if (hasNext()) {
            return this.current + 1;
        }
        return 0L;
    }

    public List<T> getRecords() {
        return this.records;
    }

    public PageResult<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public long getTotal() {
        return this.total;
    }

    public PageResult<T> total(long total) {
        this.total = total;
        return this;
    }

    public long getSize() {
        return this.size;
    }

    public PageResult<T> size(long size) {
        this.size = size;
        return this;
    }

    public long getCurrent() {
        return this.current;
    }

    public PageResult<T> current(long current) {
        this.current = current;
        return this;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }
    /**
     * <p>
     * 当前分页总页数
     * </p>
     */
    public long getPages() {
        if (getSize() == 0) {
            return 0L;
        }
        long pages = getTotal() / getSize();
        if (getTotal() % getSize() != 0) {
            pages++;
        }
        return pages;
    }

    public PageResult<T> build() {
        if (CollUtil.isEmpty(this.records)) {
            this.records = Collections.emptyList();
        }
        this.pages = this.getPages();

        return this;
    }
}
